/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  VirtAcctBalanceQueryService
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月12日
 *
 */
package cc.rengu.igas.acct.core.service.trans;

import cc.rengu.igas.acct.common.dao.MapperCommon;
import cc.rengu.igas.acct.common.entity.AcctInfo;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.igas.acct.common.util.QueryAcct3o1;
import cc.rengu.igas.acct.facade.request.VirtAcctBalanceQueryRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctBalanceQueryResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 余额查询
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/8
 */
public class VirtAcctBalanceQueryService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            VirtAcctBalanceQueryRequest virtAcctBalanceQueryRequest = new VirtAcctBalanceQueryRequest();
            ConvertUtil.convertOutput(virtAcctBalanceQueryRequest);
            rglog.info("virtAcctBalanceQueryRequest = <{}>", JSONObject.toJSONString(virtAcctBalanceQueryRequest));

            BizResponse<VirtAcctBalanceQueryResponse> bizResponse = new BizResponse<>();
            VirtAcctBalanceQueryResponse virtAcctBalanceQueryResponse = new VirtAcctBalanceQueryResponse();
            virtAcctBalanceQueryResponse.setHeader(virtAcctBalanceQueryRequest.getHeader());
            bizResponse.setResult(virtAcctBalanceQueryResponse);

            String dbPoolName = MapperCommon.getDbPoolName();
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            //查询账户信息表
            AcctInfo acctInfo = QueryAcct3o1.queryAcctInfo(virtAcctBalanceQueryRequest.getHeader().getInstId(),
                    virtAcctBalanceQueryRequest.getAcctNo(), virtAcctBalanceQueryRequest.getCustomerId(),
                    virtAcctBalanceQueryRequest.getUserType(), virtAcctBalanceQueryRequest.getUserId(), dbsUtil, rglog);
            if (null != acctInfo) {
                BeanUtil.beanCopy(acctInfo, virtAcctBalanceQueryResponse);
                virtAcctBalanceQueryResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                virtAcctBalanceQueryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(ChannelEnum.ACCT.getChannelType());
                bizResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
                ConvertUtil.convertInput(virtAcctBalanceQueryResponse);
            } else {
                throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                        AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
